Memory Management

I manage memory using a simple scheme inspired by Hanson's idea of arenas [#!hanson:90!#]. Basically, I allocate all the storage required when processing a source file (primarily for names and scraps) using calls to arena_getmem(n), where n specifies the number of bytes to be allocated. When the storage is no longer required, the entire arena is freed with a single call to arena_free(). Both operations are quite fast. @d Function p... @extern void *arena_getmem(); extern void arena_free(); @

@o arena.c @typedef struct chunk struct chunk *next; char *limit; char *avail; Chunk; @| Chunk @

We define an empty chunk called first. The variable arena points at the current chunk of memory; it's initially pointed at first. As soon as some storage is required, a ``real'' chunk of memory will be allocated and attached to first->next; storage will be allocated from the new chunk (and later chunks if necessary). @o arena.c @static Chunk first = NULL, NULL, NULL ; static Chunk *arena = &first; @| first arena @



Subsections